﻿using OvenParameterForm.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace OvenParameterForm.Util
{
    public class SqlSugarClientHelp<T> where T : SQLInfoBase, new()
    {
        public SqlSugarClient _db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = @"DataSource=ovenAndFormula.sqlite",
            DbType = DbType.Sqlite,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
            MoreSettings = new ConnMoreSettings()
            {
                SqliteCodeFirstEnableDefaultValue = true,
                SqliteCodeFirstEnableDescription = true
            }
        }, db =>
        {
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
            };
        });
        public SqlSugarClientHelp()
        {
            _db.DbMaintenance.CreateDatabase();//建库：如果不存在创建数据库存在不会重复创建
            CreateTable();
        }
        /// <summary>
        /// 创建表格,
        /// </summary>
        private void CreateTable()
        {
            _db.CodeFirst.InitTables<T>();
        }
        /// <summary>
        /// 获取所有表格数据
        /// </summary>
        /// <returns></returns>
        public List<T> QueryAll()
        {
            return _db.Queryable<T>().ToList();
        }

        public async Task<List<T>> QueryAllAsync()
        {
            return await _db.Queryable<T>().ToListAsync();
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="r"></param>
        public void InsertData(T r)
        {
            _db.Insertable(r).ExecuteCommand();
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="r"></param>
        public void UpdateData(T r)
        {
            _db.Updateable(r).ExecuteCommand();
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="r"></param>
        public void DeleteData(T r)
        {
            _db.Deleteable(r).ExecuteCommand();
        }
      /// <summary>
      /// 根据条件和属性，找出相匹配的第一个结果
      /// </summary>
      /// <param name="strcondi"></param>
      /// <param name="strpro"></param>
      /// <param name="t"></param>
        public void FindFirstByCondition(string strcondi,string strpro ,out T t)
        {
            t = _db.Queryable<T>().ToList().FirstOrDefault(f => f.GetType().GetProperty(strpro).GetValue(f).ToString()==strcondi);
        }
        /// <summary>
        /// 根据条件，找到符合条件的集合，条件类型仅限于字符串
        /// 通过反射的形式获得泛型类的属性值，匹配与属性值相同的字符串
        /// </summary>
        /// <param name="prostr">属性名称</param>
        /// <param name="constr">条件信息</param>
        /// <returns></returns>
        public List<T> FindList(string prostr, string constr)
        {
            return _db.Queryable<T>().Where(w => w.GetType().GetProperty(prostr).GetValue(w).ToString() == constr).ToList();
        }
    }
}
